package com.lvtongka.app.ui.security

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.lvtongka.app.base.BaseViewModel
import com.lvtongka.app.common.bean.AccountRequest
import com.lvtongka.app.common.bean.RealNameRequest
import com.lvtongka.app.fragment.home.HomeViewModel
import com.lvtongka.app.helper.SPHelper
import com.lvtongka.app.helper.SPKConstants
import com.lvtongka.app.http.HttpCallBack
import com.lvtongka.app.http.OkhttpUtils
import com.lvtongka.app.http.common.UrlConstants

class SecurityViewModel : BaseViewModel() {
    private val _result = MutableLiveData<Boolean>()
    val result:LiveData<Boolean> = _result

    private val _realName = MutableLiveData<Boolean>()
    val realName:LiveData<Boolean> = _realName

    fun getLogOff() {
        val request = AccountRequest("02")
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    _result.postValue(true)
                }
            }, getAccessToken())
    }

    /**
     * 实名认证
     */
    fun realNameAuthentication(name: String, idNo: String) {
        val  request = RealNameRequest(name, idNo)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    SPHelper.putBoolean(SPKConstants.IS_REAL_NAME, true)
                    _realName.postValue(true)
                }
            }, getAccessToken())
    }

}